package com.example.netactivity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements Runnable {
    private static final String TAG = "Net";
    Handler handler;
    TextView show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = findViewById(R.id.text_view);

        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 7) {
                    String str = (String) msg.obj;
                    //收到子线程返回的数据
                    Log.i(TAG, "handleMessage:收到str=" + str);
                    show.setText(str);
                    // Toast.makeText(NetActivity.this,str,Toast.LENGTH_SHORT);
                }
                super.handleMessage(msg);

            }
        };
    }
    public void onclick(View btn) {
        Log.i(TAG, "onclick:");
        Thread t = new Thread (this);
        Log.i(TAG, "onCreate:启动子线程");
        t.start();
    }
    public void run () {
        Log.i(TAG, "run:线程已运行");
        //可以访问网络资源
        //延时，模拟操作耗时
        String retStr = "";
        try {
            Log.i(TAG, "run:正在工作。。。");
            Document doc = Jsoup.connect("https://chl.cn/?jinri").get();
            Log.i(TAG, "title=" + doc.title());
            Elements tables = doc.getElementsByTag("table");
            Element table1 = tables.first();
            //Log.i(TAG, "run:table=" + table1);
            //查找tr
            Elements rows = table1.getElementsByTag("tr");

            for (Element row : rows) {
               // Log.i(TAG, "run:row=" + row);
                //拆分单元格td
                Elements tds = row.getElementsByTag("td");
                Element td1 = tds.first();
                Element td2 = tds.get(4);//第五列表格，即第一列是0开始
                Log.i(TAG, "run:币种:" + td1.text() + "价格：" + td2.text());
                Log.i(TAG, "run:币种2:" + td1.html() + "价格:" + td2.html());
                //结果->retStr
                retStr +=("币种：" + td1.text() +"=>"+ "价格：" + td2.text() + "\n");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);                                                   //!!!!
        } catch (IOException e) {
            e.printStackTrace();                                                              //!!!
            throw new RuntimeException(e);
        }
        //发送数据给主线程
        Message msg = handler.obtainMessage();
        msg.what=7;
        msg.obj = retStr;
        handler.sendMessage(msg);
    }
    private String inputStream2String (InputStream inputStream) throws IOException {
        final int bufferSize = 1024;
        final char[] buffer = new char[bufferSize];
        final StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(inputStream, "utf-8");
        while (true) {
            int rsz = in.read(buffer,0,buffer.length);
            if (rsz < 0)
                break;
            out.append(buffer, 0, rsz);
        }
        return out.toString();
    }
}